﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace JQueryRotate
{
    public class XmlHelper
    {
        public static List<Sector> GetSectorList()
        {
            XmlDocument doc=new XmlDocument();　　
            //获得配置文件的全路径　　
            string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString()+ "SectorConfig.xml";
            doc.Load(strFileName);　　
　　
            XmlNodeList sectorNodeList = doc.GetElementsByTagName("Sector");
            if (sectorNodeList == null)
                return null;

            List<Sector> sectorList = new List<Sector>();
            foreach (XmlNode sectorNode in sectorNodeList)
            {
                if(sectorNode.HasChildNodes)
                {
                    XmlNode indexNode = sectorNode.ChildNodes[0];
                    XmlNode angleNode = sectorNode.ChildNodes[1];
                    XmlNode textNode = sectorNode.ChildNodes[2];
                    XmlNode timesAvailableNode = sectorNode.ChildNodes[3];
                    XmlNode timesRecoredNode = sectorNode.ChildNodes[4];

                    Sector sector = new Sector(Convert.ToInt32(indexNode.InnerText),
                        Convert.ToDouble(angleNode.InnerText),
                        textNode.InnerText,
                        Convert.ToInt32(timesAvailableNode.InnerText),
                        Convert.ToInt32(timesRecoredNode.InnerText)
                        );
                    sectorList.Add(sector);
                }
            }

            return sectorList;
        }

        public static void GotOneTime(int index)
        {
            XmlDocument doc=new XmlDocument();　　
            //获得配置文件的全路径　　
            string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString()+ "SectorConfig.xml";
            doc.Load(strFileName);

            XmlNodeList sectorNodeList = doc.GetElementsByTagName("Sector");
            if (sectorNodeList == null)
                return ;

            List<Sector> sectorList = new List<Sector>();
            foreach (XmlNode sectorNode in sectorNodeList)
            {
                if (sectorNode.HasChildNodes)
                {
                    XmlNode indexNode = sectorNode.ChildNodes[0];
                    XmlNode timesNode = sectorNode.ChildNodes[3];
                    XmlNode timesRecordNode = sectorNode.ChildNodes[4];

                    if (index == Convert.ToInt32(indexNode.InnerText))
                    {
                        //int times = Convert.ToInt32(timesNode.InnerText);
                        //times--;
                        //timesNode.InnerText = times.ToString();

                        int timesRecord = Convert.ToInt32(timesRecordNode.InnerText);
                        timesRecord++;
                        timesRecordNode.InnerText = timesRecord.ToString();

                        break;
                    }
                }
            }
            try
            {
                //保存上面的修改　　  
                doc.Save(strFileName);

            }
            catch (Exception e)
            {
                throw e;
            }

        }
    }
}